import time

import openai
import pandas as pd


def ask_chatgpt(question):
    # 发送用户输入的问题到 ChatGPT
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": question}
        ]
    )

    # 获取 ChatGPT 的回复
    return response.choices[0].message.content


# 设置 OpenAI API 密钥
openai.api_key = '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'

# 设置代理
# proxies = {'http': "http://127.0.0.1:10809",
#            'https': "http://127.0.0.1:10809"}
proxies = {'http': "http://127.0.0.1:21002",
           'https': "http://127.0.0.1:21002"}

openai.proxy = proxies

print(ask_chatgpt("write a poem"))

# # 读文件
# data = pd.read_csv('data.csv')
# i = 0
#
# # 遍历每一行，调用 ask_chatgpt()
# for index, row in data.iterrows():
#     if (i % 3 == 0 and i != 0):
#         print("I'm sleeping, finish: " + str(i))
#         time.sleep(60)
#     prompt = "Please generate a short comment for the following function:\n"
#     question = prompt + row.iloc[1]  # 第2列作为问题
#     reply = ask_chatgpt(question)  # 调用 ask_chatgpt() 获取回复
#     data.at[index, 'chatgpt注释'] = reply
#     i += 1
#
# # 将更新后的数据保存到新的CSV文件
# data.to_csv('output.csv', index=False)
